package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.ConfigFileBean;
import com.hzdesk.media.koreabatchmatvector.config.RBConfig;
import com.hzdesk.media.koreabatchmatvector.inter.NetworkRequestListener;
import com.hzdesk.media.koreabatchmatvector.logic.contract.SystemSettingsContract;
import com.hzdesk.media.koreabatchmatvector.logic.model.SystemSettingsModle;
import com.hzdesk.media.koreabatchmatvector.request.NetworkRequest;
import com.hzdesk.media.koreabatchmatvector.utils.JsonUitl;
import com.hzdesk.media.koreabatchmatvector.utils.MyToastUtils;

import org.json.JSONObject;

import okhttp3.Response;

/**
 * Created by Administrator on 2018/7/9.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class SystemSettingsPersnter implements SystemSettingsContract.ISystemSettingsPersnter {

    private SystemSettingsContract.ISystemSettingsView iSystemSettingsView;
    private SystemSettingsContract.ISystemSettingsModle iSystemSettingsModle ;
    private Context mContext ;
    public SystemSettingsPersnter(SystemSettingsContract.ISystemSettingsView iSystemSettingsView) {
        this.iSystemSettingsView = iSystemSettingsView;
        iSystemSettingsModle = new SystemSettingsModle();
    }
    @Override
    public void initDate(Context tContext) {
        mContext = tContext;
        //EtName.setOnFocusChangeListener(onFocusChangeListener);
    }

    @Override
    public void ConfigFileManger(Context mContext) {
        // todo 弹进度
        MyToastUtils.ShortToaseShow(mContext,"配置文件下载好了");
        NetworkRequest mNetworkRequest =   new NetworkRequest();
        mNetworkRequest.setmNetworkRequestListener(mNetworkRequestListener);
        mNetworkRequest.requestServerDate(mContext, RBConfig.DEVICE_ATTRIBUTE);
    }

    @Override
    public void SaveMeetName(Context mContext) {

    }

    @Override
    public void onClickedChangeMeetingName() {
        iSystemSettingsView.toChangeMeetingNamePager();
    }

    @Override
    public void onClickedNotation() {
        iSystemSettingsView.todNotationPager();
    }

    @Override
    public void onClickedSetLoge() {
        iSystemSettingsView.toSetLogePager();
    }

    @Override
    public void onclickedBlockLength() {
        iSystemSettingsView.toBlockLengthPager();
    }

    @Override
    public void onclickedRecordingMode() {
        iSystemSettingsView.toRecordingModePager();
    }

    @Override
    public void onclickedScreenshotsPath() {
        iSystemSettingsView.toScreenshotsPathPager();
    }

    @Override
    public void onclickedVidoPath() {
        iSystemSettingsView.toVidoPathpAGER();
    }

    @Override
    public void onclickedModel() {
        iSystemSettingsView.toModelPager();
    }

    @Override
    public void onclickedBindCulde() {
        iSystemSettingsView.toBindCuldePager();
    }

    @Override
    public void onclickedAbout() {
        iSystemSettingsView.toAboutPager();
    }

    @Override
    public void onCliackedBack() {
        iSystemSettingsView.toFinshPager();
    }


    @Override
    public void onClickChangeProtocal() {
        iSystemSettingsView.toProtocalPager();

    }

    @Override
    public void setOnHoverListenerAndOnFocusChangeListener(LinearLayout linearLayout) {
        linearLayout.setOnHoverListener(onHoverListener);
        linearLayout.setOnFocusChangeListener(onFocusChangeListener);
    }

    @Override
    public void onclickedMeetingName() {
        iSystemSettingsView.toMeetingNamePage();

    }

    @Override
    public void onClickedNetWork() {
        iSystemSettingsView.toNetWorkPager();
    }


    // 管理配置文件
    private NetworkRequestListener mNetworkRequestListener = new NetworkRequestListener(){
        @Override
        public void success(Response response) {
            try {
                String NetWorkResponse = response.body().string();
                final JSONObject jsonObject = new JSONObject(NetWorkResponse);
                int Code = JsonUitl.getParsinCode(jsonObject);
                if(Code == 100){
                    String normalDate = JsonUitl.getStringDateFeld(jsonObject);
                    ConfigFileBean mConfigFileBean = new Gson().fromJson(normalDate,ConfigFileBean.class);
                    iSystemSettingsModle.SaveConfigFileDate(mConfigFileBean,mContext);              //  存储数据库
                    iSystemSettingsView.meetName(mConfigFileBean.getMeetName());                    //  展示会议名称
                }else{
                    MyToastUtils.ShortToaseShow(mContext,jsonObject.getString("data"));
                }
            } catch (Exception e) {}

        }
        @Override
        public void fail(String s) {
            Log.e("SetPersenter" ,"fail: "+s);
        }
    };
    //鼠标移动事件
    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    //v.clearFocus();
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };
    //焦点改变事件
    private View.OnFocusChangeListener onFocusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LinearLayout linearLayout= (LinearLayout) v;
            if (hasFocus){
                linearLayout.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            }else {
                linearLayout.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };

}
